package com.yun.interceptor;

import com.yun.utils.UserUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        String url = request.getRequestURI();
        if (url.contains("login.action") ||
                url.contains("register.action") ||
                url.contains("share.action") ||
                url.contains("getShareFiles.action") ||
                url.contains("download.action") ||
                url.contains("goRegister.action") ||
                url.contains("findUsername.action") ||
                url.contains("goLogin.action") ||
                url.contains("callback.action") ||
                url.contains("githubLogin.action") ||
                url.contains("githubRegister.action") ||
                url.contains("adminLogin.action") ||
                url.contains("getAdminByName.action") ||
                url.contains("adminList.action") ||
                url.contains("deleteUser.action")) {
            return true;
        }

        String username = UserUtils.getUsername(request);

        if (username != null) {
            return true;
        }

        response.sendRedirect("/SharedCloud_war_exploded/user/goLogin.action");
        return false;
    }

}
